#include "PrecompiledHeaders.h"
#include "Thread.h"
#include <stdio.h>
#include <stdlib.h>

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32

// windows version of Thread class
#include <process.h>

Thread::Thread(void thread_function(void*))
{
    if(_beginthread(thread_function, 0, NULL) == -1) {
        fprintf(stderr,"ERROR: could not _beginthreadex\n");
        exit(-1);
    }
}

void Thread::stop(void)
{
    _endthreadex(NULL);
}

#else

// linux version of the Thread class
ThreadFunction Thread::t_f = NULL;

Thread::Thread(ThreadFunction thread_function)
{
    this->t_f = thread_function;
    int rc;
    if((rc = pthread_create(&thread, NULL, &Thread::temp, &thread_function))) {
        fprintf(stderr,"ERROR; return code from pthread_create() is %i\n",rc);
        exit(-1);
    }
}

void Thread::stop(void)
{
    pthread_exit(NULL);
}

void *Thread::temp(void *args)
{
    if(args) (*((ThreadFunction)args))(NULL);
    return NULL;
}

#endif

Thread::~Thread(void) {}

